import { Location, LocationStrategy, PathLocationStrategy } from '@angular/common';
import { Component, OnInit } from '@angular/core';
// import { ToastService, UploadConfig } from 'ng-tui';
import { GlobalService, AuthService } from '../../../../cores/services';
import { SupplierService } from '../../services/supplier.service';
import { Manage } from '../../interfaces/manage.interface';
import { ActivatedRoute, Router } from '@angular/router';
import { Pagination, ConfirmService, ToastService } from 'ng-tui';
import { ApiData, SearchParams } from '../../../../cores/classes';
import { Goods } from './../../interfaces/manage.interface';
import { skipWhile, switchMap } from 'rxjs/operators';

@Component({
    templateUrl: './supplier-table.component.html',
    providers: [Location, { provide: LocationStrategy, useClass: PathLocationStrategy }],
})
export class SupplierTableComponent implements OnInit {


    theads = ['#', '供应商编号', '供应商名称', '供应商地址', '所属采购员', '业务联系人', '业务联系电话', '物流联系人', '物流联系电话', '应付账款', '预付账款', '带垫余额', '基本信息', '财务信息'
        , '附件', '操作'];
    theadtwo = ['#', '开票名称', '发票类型', '公司地址', '税号', '银行账号', '开户行名称', '税票地址', '税票电话'];
    theadfix = ['#', '登入账号', '初始密码', '登入限制', '账号状态', '模块', '收费'];
    theadthree = ['#', '营业执照', '行业许可证', '法人身份证'];
    search = new SearchParams({ start: '', end: '' });

    list = new Array<Goods>();

    loading = false;

    pagination = new Pagination();

    constructor(
        private activatedRoute: ActivatedRoute,
        private router: Router,
        private confirm: ConfirmService,
        private toast: ToastService,
        public global: GlobalService,
        private supplierService: SupplierService
    ) { }

    ngOnInit() {
        this.activatedRoute.url
            .pipe(skipWhile(() => this.router.url !== '/manage/account'))
            .subscribe(() => this.loadDatas());
    }

    // doSearch() {
    //     this.pagination.reset();
    //     this.loadDatas();
    // }

    // doReset() {
    //     this.search.clean();
    //     this.doSearch();
    // }

    loadDatas() {
        this.loading = true;
        this.supplierService.searchGoods(this.pagination, this.search)
            .subscribe({
                next: res => {
                    this.pagination.total = res.datas.total;
                    this.list = res.datas.rows;
                },
                complete: () => this.loading = false
            });
    }

    // confirmDelete(goods: Goods) {
    //     this.confirm.danger('删除确认', `您确认删除商品'${goods.goodsName}'吗？`)
    //         .pipe(switchMap<void, ApiData>(() => this.manageService.deleteGoods(goods.id)))
    //         .subscribe(() => {
    //             this.list.splice(this.list.indexOf(goods), 1);
    //             this.toast.success('删除成功', `成功删除商品'${goods.goodsName}`);
    //             this.loadDatas();
    //         });
    // }

}
